<template>
  <div class="content" v-loading="ishow">
    <el-card>
      <el-row>
        <el-form label-width="70px" size="small">
          <el-form-item label="关键字">
            <el-input v-model="params.keyWord" placeholder="请输班级姓名或者铺导员姓名" style="width: 300px;" clearable
              size="small"></el-input>
            <el-button type="primary" style="margin: 0 10px;" @click="getList" size="small"
              v-if="$hasBP('btn.class.select')">查询</el-button>
            <el-button type="success" @click="addClass" size="small" v-if="$hasBP('btn.class.add')">增加</el-button>
          </el-form-item>
        </el-form>

      </el-row>

      <el-row style="margin-top: 10px;">
        <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" border>
          <el-table-column prop="claNum" label="班级编号" align="center">
          </el-table-column>
          <el-table-column prop="claName" label="班级名称" align="center">
          </el-table-column>
          <el-table-column prop="claPrice" label="剩余班费" align="center">
            <template #default="{row,$index}">
              {{row.claPrice}}元
            </template>
          </el-table-column>
          <el-table-column prop="teaName" label="辅导员" align="center">
            <template #default="{row,$index}">
              {{row.teaName==null?'暂无':row.teaName}}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template #default="{row,$index}">
              <el-button type="warning" @click="updateClass(row)" size="small"
                v-if="$hasBP('btn.class.update')">修改</el-button>
              <el-button type="danger" @click="delClass(row)" size="small" v-if="$hasBP('btn.class.del')">删除</el-button>
              <el-button size="small" type="primary" @click="gotoDetail(row)">
                查看明细
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-row>

      <!-- v-if="total>5" -->
      <el-row>
        <div style="float: right;margin-top: 10px;">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="params.pageNum" :page-sizes="[1, 5, 10]" :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </el-row>
    </el-card>


    <el-dialog title="班级操作" :visible.sync="isAddorUpdateShow" width="30%">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="班级编号">
          <el-input v-model="form.claNum" size="small"></el-input>
        </el-form-item>
        <el-form-item label="班级名称">
          <el-input v-model="form.claName" size="small"></el-input>
        </el-form-item>
        <el-form-item label="班级班费">
          <el-input v-model="form.claPrice" size="small"></el-input>
        </el-form-item>
        <el-form-item label="辅导员">
          <el-select v-model="form.teaId" placeholder="请选择" size="small">
            <el-option v-for="item in teacherList" :key="item.id" :label="item.teaName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isAddorUpdateShow = false">取 消</el-button>
        <el-button type="primary" @click="affirm()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'class',
    data() {
      return {
        params: {
          pageNum: 1,
          pageSize: 5,
          keyWord: ""
        },
        tableData: [],
        isAddOrUpdate: 0,//0添加1修改
        isAddorUpdateShow: false,
        form: {
          claPrice: 0
        },
        teacherList: [],
        total: 0,
        ishow: true
      };
    },

    mounted() {
      this.getList()
      this.getTeacherList()
    },

    methods: {
      getList() {
        this.$api.cla.getList(this.params).then(res => {
          console.log(res);
          if (res.code == 200) {
            this.tableData = res.data
            this.total = res.pageParams.total
            this.ishow = false
          }
        })
      },
      getTeacherList() {
        this.$api.common.selectListofTeacher().then(res => {
          if (res.code == 200) {
            this.teacherList = res.data
          }
        })
      },
      affirm() {
        if (this.form.claNum == undefined || this.form.claNum.trim() == '') {
          this.$message.warring("请输入班级编号")
          return
        }

        if (this.form.claName == undefined || this.form.claName.trim() == '') {
          this.$message.warring("请输入班级名称")
          return
        }
        if (this.form.teaId == undefined || this.form.teaId == null) {
          this.$message.warring("请选择辅导员")
          return
        }
        if (this.isAddOrUpdate == 0) {//添加
          this.$api.cla.addClass(this.form).then(res => {
            if (res.code == 200) {
              this.$message.success("添加成功~")
              this.getList()
              this.isAddorUpdateShow = false
            }
          })
        } else if (this.isAddOrUpdate == 1) {
          this.$api.cla.updateClass(this.form).then(res => {
            if (res.code = 200) {
              this.$message.success("修改成功~")
              this.getList()
              this.isAddorUpdateShow = false
            }
          })

        }
      },
      addClass() {
        this.isAddOrUpdate = 0;
        this.isAddorUpdateShow = true;
        this.form = {}
      },
      updateClass(data) {
        console.log(data);
        this.form = JSON.parse(JSON.stringify(data))
        this.isAddorUpdateShow = true
        this.isAddOrUpdate = 1
      },
      handleSizeChange(val) {
        console.log(val);
        this.params.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.params.pageNum = val
        this.getList()
      },
      delClass(row) {
        console.log(row);
        this.$confirm('此操作将永久删除数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(res => {
          this.$api.cla.delClass(row.id).then(res => {
            console.log(res);
            if (res.code == 200) {
              this.$message.success("删除成功~")
              this.getList()
            }
          })
        }).catch(() => { })
      },
      gotoDetail(row) {
        console.log(this.$router);
        this.$router.push('/class/detail?id=' + row.id);
      }
    },
  };
</script>

<style lang="scss" scoped>
  .content {
    padding: 10px;
  }
</style>